//
//  KoiNavigationController.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 12.04.25.
//

import Foundation
//extension UINavigationController: UIGestureRecognizerDelegate {
//    override open func viewDidLoad() {
//        super.viewDidLoad()
//        interactivePopGestureRecognizer?.delegate = self
//    }
//
//    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
//        return viewControllers.count > 1
//    }
//}

import UIKit
import ObjectiveC

class GestureRecognizerDelegate: NSObject, UIGestureRecognizerDelegate {

    weak var navigationController: UINavigationController?

    init(navigationController: UINavigationController) {

        self.navigationController = navigationController

    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

        return navigationController?.viewControllers.count ?? 0 > 1

    }

}

private var gestureDelegateKey: UInt8 = 0

extension UINavigationController {

    override open func viewDidLoad() {

        super.viewDidLoad()

        let delegate = GestureRecognizerDelegate(navigationController: self)

        interactivePopGestureRecognizer?.delegate = delegate

        objc_setAssociatedObject(self, &gestureDelegateKey, delegate, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

    }

}
